﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Runtime.CompilerServices;

namespace WP8DataModel.Model
{
    public class BackgroundScheduleDataContext : DataContext
    {
        // Pass the connection string to the base class.
        public BackgroundScheduleDataContext(string connectionString)
            : base(connectionString)
        { }

        // Specify a table for the to-do items.
        public Table<BackgroundScheduledTransferItem> BackgroundScheduleItems;

    }

    [Table]
    public class BackgroundScheduledTransferItem : INotifyPropertyChanged, INotifyPropertyChanging
    {
        // Define ID: private field, public property, and database column.
        private int _id;

        //TODO: rewrite it to support cool .net4.5 feature of the attribute name
        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int Id
        {
            get { return _id; }
            set
            {
                SetField(ref _id, value);
            }
        }

        private int _stationId;
        [Column]
        public int StationId
        {
            get { return _stationId; }
            set
            {
                SetField(ref _stationId, value);
            }
        }

        private long _showId;
        [Column]
        public long ShowId
        {
            get { return _showId; }
            set
            {
                SetField(ref _showId, value);
            }
        }

        // Define item name: private field, public property, and database column.
        private string _stationTitle;

        [Column]
        public string StationTitle
        {
            get { return _stationTitle; }
            set
            {
                SetField(ref _stationTitle, value);
            }
        }

        private string _showTitle;

        [Column]
        public string ShowTitle
        {
            get { return _showTitle; }
            set
            {
                SetField(ref _showTitle, value);
            }
        }


        private bool _autoPlay;

        [Column]
        public bool AutoPlay
        {
            get { return _autoPlay; }
            set
            {
                SetField(ref _autoPlay, value);
            }
        }

        private bool _downloadLater;

        [Column]
        public bool DownloadLater
        {
            get { return _downloadLater; }
            set
            {
                SetField(ref _downloadLater, value);
            }
        }

        private bool _autoStop;

        [Column]
        public bool AutoStop
        {
            get { return _autoStop; }
            set
            {
                SetField(ref _autoStop, value);
            }
        }

        private DateTime _startTime;

        [Column]
        public DateTime StartTime
        {
            get { return _startTime; }
            set
            {
                SetField(ref _startTime, value);
            }
        }

        private DateTime _stopTime;

        [Column]
        public DateTime StopTime
        {
            get { return _stopTime; }
            set
            {
                SetField(ref _stopTime, value);
            }
        }

        private string _backgroundTransferRequestId;

        [Column]
        public string BackgroundTransferRequestId
        {
            get { return _backgroundTransferRequestId; }
            set
            {
                SetField(ref _backgroundTransferRequestId, value);
            }
        }

        private bool _movedToMediaLibrary;

        [Column]
        public bool MovedToMediaLibrary
        {
            get { return _movedToMediaLibrary; }
            set
            {
                SetField(ref _movedToMediaLibrary, value);
            }
        }

        //Let's hope, this fields will always be empty :)
        private string _errorMessage;

        [Column]
        public string ErrorMessage
        {
            get { return _errorMessage; }
            set
            {
                SetField(ref _errorMessage, value);
            }
        }

        private string _downloadStatus;
        public string DownloadStatus
        {
            get { return _downloadStatus; }
            set
            {
                SetField(ref _downloadStatus, value);
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion


        protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, value))
            {
                return false;
            }
            NotifyPropertyChanging(propertyName);
            field = value;
            NotifyPropertyChanged(propertyName);
            return true;
        }



        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }
}
